Shader "Custom/FireDistortionUI"
{
    Properties
    {
        [HideInInspector] [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
        [HideInInspector]_Color("Tint", Color) = (1,1,1,1)
        _NoiseTex("噪声纹理", 2D) = "white" {}
        _Distortion("扭曲程度", Range(-0.1, 0.1)) = 0.02
        _Frequency("变幻频率", Range(0, 5)) = 2.0

            // 呼吸效果属性
            _BreathColor("呼吸颜色", Color) = (1,0.5,0,0.5)
            _BreathIntensity("呼吸强度", Range(0, 2)) = 0.5 // 新增：控制呼吸效果强度，0-2范围支持更强效果
            _CirclePosition("圆形位置", Vector) = (0.5,0.5,0,0)
            _CircleSize("圆形大小", Range(0, 2)) = 1.0
            _TransitionSmoothness("过渡平滑度", Range(0.01, 1)) = 0.2
            _BreathFrequency("呼吸频率", Range(0.1, 10)) = 1.0
            _BreathMinIntensity("呼吸最小值", Range(0, 0.9)) = 0.2

            // 图集UV矩形（由C#脚本设置）
            [HideInInspector]_UvRect("UV Rect", Vector) = (0,0,1,1)

            // UI默认属性
            [HideInInspector]_StencilComp("Stencil Comparison", Float) = 8
            [HideInInspector]_Stencil("Stencil ID", Float) = 0
            [HideInInspector]_StencilOp("Stencil Operation", Float) = 0
            [HideInInspector]_StencilWriteMask("Stencil Write Mask", Float) = 255
            [HideInInspector]_StencilReadMask("Stencil Read Mask", Float) = 255
            [HideInInspector]_ColorMask("Color Mask", Float) = 15
            [HideInInspector][Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0

            _UnscaledTimeBool("不受时间暂停", Range(0, 1)) = 0
            _UnscaledTime("Unscaled Time", Float) = 0.0 // 外部传入 Time.unscaledTime
    }

        SubShader
        {
            Tags
            {
                "Queue" = "Transparent"
                "IgnoreProjector" = "True"
                "RenderType" = "Transparent"
                "PreviewType" = "Plane"
                "CanUseSpriteAtlas" = "True"
            }

            Stencil
            {
                Ref[_Stencil]
                Comp[_StencilComp]
                Pass[_StencilOp]
                ReadMask[_StencilReadMask]
                WriteMask[_StencilWriteMask]
            }

            Cull Off
            Lighting Off
            ZWrite Off
            ZTest[unity_GUIZTestMode]
            Blend SrcAlpha OneMinusSrcAlpha
            ColorMask[_ColorMask]

            Pass
            {
                Name "Default"
            CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma target 2.0

                #include "UnityCG.cginc"
                #include "UnityUI.cginc"

                #pragma multi_compile_local _ UNITY_UI_CLIP_RECT
                #pragma multi_compile_local _ UNITY_UI_ALPHACLIP

                struct appdata_t
                {
                    float4 vertex   : POSITION;
                    float4 color    : COLOR;
                    float2 texcoord : TEXCOORD0;
                    UNITY_VERTEX_INPUT_INSTANCE_ID
                };

                struct v2f
                {
                    float4 vertex   : SV_POSITION;
                    fixed4 color : COLOR;
                    float2 texcoord  : TEXCOORD0;
                    float4 worldPosition : TEXCOORD1;
                    UNITY_VERTEX_OUTPUT_STEREO
                };

                sampler2D _MainTex;
                sampler2D _NoiseTex;
                float4 _MainTex_ST;
                float4 _NoiseTex_ST;
                fixed4 _Color;
                float _Distortion;
                float _Frequency;

                fixed4 _BreathColor;
                float _BreathIntensity; // 新增：呼吸强度控制变量
                float2 _CirclePosition;
                float _CircleSize;
                float _TransitionSmoothness;
                float _BreathFrequency;
                float _BreathMinIntensity;

                float4 _ClipRect;
                float4 _UvRect; // 精灵在图集中的UV矩形（x:minU, y:minV, z:maxU, w:maxV）
                half _UnscaledTimeBool, _UnscaledTime;
                v2f vert(appdata_t v)
                {
                    v2f OUT;
                    UNITY_SETUP_INSTANCE_ID(v);
                    UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
                    OUT.worldPosition = v.vertex;
                    OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);

                    OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                    OUT.color = v.color * _Color;

                    return OUT;
                }

                fixed4 frag(v2f IN) : SV_Target
                {
                    // ========== 步骤1：全局UV → 精灵局部UV（0-1范围） ==========
                    float2 localUV = (IN.texcoord - float2(_UvRect.x, _UvRect.y)) /
                                     float2(_UvRect.z - _UvRect.x, _UvRect.w - _UvRect.y);

                // 安全检查：确保局部UV在0-1范围内（防止无效UV导致的黑边）
                if (localUV.x < 0 || localUV.x > 1 || localUV.y < 0 || localUV.y > 1)
                {
                    return fixed4(0, 0, 0, 0);
                }

                // ========== 步骤2：在局部UV上应用噪声扭曲 ==========
                float2 noiseUV = localUV * _NoiseTex_ST.xy + _NoiseTex_ST.zw;
                half currentTime = lerp(_Time.y, _UnscaledTime, _UnscaledTimeBool);
                noiseUV.y += currentTime * _Frequency; // 垂直移动模拟火焰上升
                fixed4 noise = tex2D(_NoiseTex, noiseUV);
                float2 distortion = (noise.rg * 2 - 1) * _Distortion;
                float2 distortedLocalUV = localUV + distortion;

                // 安全检查：扭曲后局部UV仍需在0-1范围内
                if (distortedLocalUV.x < 0 || distortedLocalUV.x > 1 ||
                    distortedLocalUV.y < 0 || distortedLocalUV.y > 1)
                {
                    return fixed4(0, 0, 0, 0);
                }

                // ========== 步骤3：扭曲后的局部UV → 全局UV（反推） ==========
                float2 distortedGlobalUV = float2(_UvRect.x, _UvRect.y) +
                                           distortedLocalUV * float2(_UvRect.z - _UvRect.x, _UvRect.w - _UvRect.y);

                // ========== 步骤4：采样主纹理并应用颜色 ==========
                half4 color = tex2D(_MainTex, distortedGlobalUV) * IN.color;

                // ========== 呼吸效果（基于局部UV，确保位置一致） ==========
                float distance = length(localUV - _CirclePosition);
                float baseIntensity = (sin(_Time.y * _BreathFrequency) + 1) * 0.5;
                float breathIntensity = lerp(_BreathMinIntensity, 1.0, baseIntensity);
                float circle = 1 - smoothstep(
                    _CircleSize * (1 - _TransitionSmoothness) * breathIntensity,
                    _CircleSize * breathIntensity,
                    distance
                );

                // 修改：使用新增的_BreathIntensity控制混合强度，默认值0.5保持原有效果
                color.rgb = lerp(color.rgb, color.rgb + _BreathColor.rgb * circle, _BreathIntensity);

                // ========== UI裁剪（保持原有逻辑） ==========
                #ifdef UNITY_UI_CLIP_RECT
                color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
                #endif

                #ifdef UNITY_UI_ALPHACLIP
                clip(color.a - 0.001);
                #endif

                return color;
            }
        ENDCG
        }
        }
}
